<template>
	<view class="release">
		<cu-custom bgColor="bg-gradual-purple" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">我的消息</block>
		</cu-custom>
		<Loading :loadModal="loadModal" :hint="hint"></Loading>
		<view class="cu-list menu  menu-avatar" v-if="notice.length>0">
			<view class="cu-item" v-for="(item,idx) in notice" :key="idx" :data-idx="idx" :data-path="item.path"
				@tap="bindDetail">
				<view class="cu-avatar round lg">
					<text :class="'cuIcon-'+item.icon+' '+'text-'+item.color"></text>
				</view>
				<view class='content'>
					<view>{{item.tag}}</view>
					<view class='text-gray text-sm'>
						{{item.title}}
					</view>
				</view>
				<view class='action'>
					<view class='text-grey text-xs'>{{item.datetime}}</view>
				</view>
			</view>
		</view>
		<Nodata :noModal="nodata"></Nodata>
	</view>
</template>

<script>
	import Loading from '../../components/loading.vue';
	import Nodata from '../../components/nodata.vue';
	import util from '../../utils/util.js';
	import api from '../../utils/api.js';
	
	export default {
		components: {
			Loading,
			Nodata
		},
		data() {
			return {
				imgUrl: getApp().globalData.imgUrl,
				loadModal: false, //加载
				hint: '加载中...', //加载提示
				nodata: false,
				notice: [],
				page: 1,
			}
		},

		onLoad: async function(options) {
			uni.setStorageSync('showRedDot', '1');
			await this.getNoticeLogsList();
		},
		methods: {
			/**
			 * 消息详情
			 */
			bindDetail: function(e) {
				let path = e.currentTarget.dataset.path;
				console.info(path)
				uni.navigateTo({
					url: path
				})
			},
			/**
			 * 获取消息列表
			 */
			getNoticeLogsList: async function(refresh = '') {
				this.hint = refresh == 'below' ? '刷新中...' : '加载中...';
				if (refresh == 'below' || this.page == 1) {
					this.notice = [];
				} //清空列表
				this.loadModal = true;
				let result = await api.getNoticeLogsList(this.page, '');
				this.loadModal = false;
				uni.stopPullDownRefresh();
				let list = result.data;
				this.notice = this.page == 1 ? list : [...this.notice, ...list];
				this.nodata = list.length === 0 && this.page == 1 ? true : false;
				if (list.length == 0) {
					uni.showToast({
						title: '没有更多了！',
						icon: 'none',
						duration: 2000
					})
				}
			}
		},
		onPullDownRefresh: async function() { //下拉刷新
			this.page = 1;
			await this.getNoticeLogsList('below');
			// console.log('onPullDownRefresh');
		},
		onReachBottom: async function() { //上拉加载
			this.page = this.page + 1;
			await this.getNoticeLogsList('up');
			// console.log('onReachBottom');
		},
	}
</script>

<style>
.margin-top-self {
    margin-top: 120rpx;
  }
</style>
